package steamGameAll

import (
	"encoding/json"
	"strings"
	"utilsx/cont"
	"utilsx/tools"
	gameObj "webFull/model/entity/gen/game"
)

type ResultGameSourceTemp struct {
	Success bool `json:"success"`
	Data    struct {
		SupportedLanguages string `json:"supported_languages"`
		*gameObj.GameObj
	} `json:"data"`
}
type ResultGameSourceSingle = map[string]ResultGameSourceTemp
type ResultGameSource = []ResultGameSourceSingle

func GameSingleByteToSource2(single []byte) (*gameObj.GameObj, bool) {
	var sss ResultGameSourceSingle
	err := cont.Json.Unmarshal(single, &sss)
	if err != nil {
		panic(err)
	}
	for _, temp := range sss {
		marshal, err := json.Marshal(&temp.Data)
		if err != nil {
			panic(err)
			return nil, false
		}
		_, g := tools.JsonProtoToObj[*gameObj.GameObj](marshal)
		if g.Genres == nil {
			g.Genres = []*gameObj.Genre{}
		}
		return g, true

	}
	return nil, false

}

func LanguageHandle(sl string) map[string]*gameObj.Language {
	all := strings.TrimSpace(sl)
	all = strings.ReplaceAll(all, "<strong>", "")
	all = strings.ReplaceAll(all, "<\\/strong>", "")
	all = strings.ReplaceAll(all, "</strong>", "")
	all = strings.ReplaceAll(all, "*", "")
	all = strings.ReplaceAll(all, "languages with full audio support", "")
	all = strings.ReplaceAll(all, "<br>", "")
	all = strings.ReplaceAll(all, " ", "")
	all = strings.ReplaceAll(all, ",", ";")
	split := strings.Split(all, ";")
	return GameLanguages(split...)
}
func GameLanguages(splits ...string) map[string]*gameObj.Language {
	mm := make(map[string]*gameObj.Language)
	for _, v := range splits {
		mm[v] = &gameObj.Language{Interface: true, FullAudio: true, Subtitles: false}
		if strings.Contains(v, "e") {
			mm[v].Subtitles = false
			mm[v].FullAudio = true
		}
		if strings.Contains(v, "a") {
			mm[v].Subtitles = false
			mm[v].FullAudio = false
		}
	}
	return mm
}
